<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="为什么出现卡顿屏幕显示图像是需要CPU和GPU结合工作。CPU 负责计算显示内容，包括视图创建、布局计算、图片解码、文本绘制等，CPU 完成计算后，会将计算内容提交给 GPU；GPU 进行变换、合成、渲染，将渲染结果提交到帧缓冲区，当下一次垂直同步信号（简称 V-Sync）到来时，将渲染结果显示到屏幕上。 UI视图显示到屏幕中的过程： 在屏幕显示图像前，CPU 和 GPU 需要完成自身的任务，系统">
<meta property="og:type" content="article">
<meta property="og:title" content="一次卡顿优化">
<meta property="og:url" content="http://example.com/2022/08/20/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/index.html">
<meta property="og:site_name" content="Shi">
<meta property="og:description" content="为什么出现卡顿屏幕显示图像是需要CPU和GPU结合工作。CPU 负责计算显示内容，包括视图创建、布局计算、图片解码、文本绘制等，CPU 完成计算后，会将计算内容提交给 GPU；GPU 进行变换、合成、渲染，将渲染结果提交到帧缓冲区，当下一次垂直同步信号（简称 V-Sync）到来时，将渲染结果显示到屏幕上。 UI视图显示到屏幕中的过程： 在屏幕显示图像前，CPU 和 GPU 需要完成自身的任务，系统">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E8%A7%86%E5%9B%BE%E6%B8%B2%E6%9F%93%E6%B5%81%E7%A8%8B.png">
<meta property="og:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E5%8D%A1%E9%A1%BF%E5%88%86%E6%9E%90%E5%B7%A5%E5%85%B7.png">
<meta property="og:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E5%8D%A1%E9%A1%BF%E6%AF%94%E4%B8%A5%E9%87%8D%E7%A8%8B%E5%BA%A6%E8%A7%86%E5%9B%BE.png">
<meta property="og:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E8%80%97%E6%97%B6%E5%88%86%E6%9E%90%E5%B7%A5%E5%85%B7.png">
<meta property="og:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/yemian.png">
<meta property="og:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E9%A1%B5%E9%9D%A2%E5%8D%A1%E9%A1%BF%E6%83%85%E5%86%B5.png">
<meta property="og:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E6%9F%90%E6%AC%A1%E5%8D%A1%E9%A1%BF%E5%9B%BE.png">
<meta property="og:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E6%9C%AC%E5%9C%B0%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD.png">
<meta property="og:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E4%BC%98%E5%8C%96%E6%95%88%E6%9E%9C.png">
<meta property="article:published_time" content="2022-08-20T08:59:46.000Z">
<meta property="article:modified_time" content="2023-05-30T02:53:41.338Z">
<meta property="article:author" content="Shi">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E8%A7%86%E5%9B%BE%E6%B8%B2%E6%9F%93%E6%B5%81%E7%A8%8B.png">

<link rel="canonical" href="http://example.com/2022/08/20/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>一次卡顿优化 | Shi</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Shi</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/08/20/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Shi">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Shi">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          一次卡顿优化
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-08-20 16:59:46" itemprop="dateCreated datePublished" datetime="2022-08-20T16:59:46+08:00">2022-08-20</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/source/categories/iOS%E4%BC%98%E5%8C%96/" itemprop="url" rel="index"><span itemprop="name">iOS优化</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="为什么出现卡顿"><a href="#为什么出现卡顿" class="headerlink" title="为什么出现卡顿"></a>为什么出现卡顿</h1><p>屏幕显示图像是需要CPU和GPU结合工作。CPU 负责计算显示内容，包括视图创建、布局计算、图片解码、文本绘制等，CPU 完成计算后，会将计算内容提交给 GPU；GPU 进行变换、合成、渲染，将渲染结果提交到帧缓冲区，当下一次垂直同步信号（简称 V-Sync）到来时，将渲染结果显示到屏幕上。</p>
<p>UI视图显示到屏幕中的过程：<br><img src="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E8%A7%86%E5%9B%BE%E6%B8%B2%E6%9F%93%E6%B5%81%E7%A8%8B.png"></p>
<p>在屏幕显示图像前，CPU 和 GPU 需要完成自身的任务，系统会每（1000/60=16.67ms）将UI的变化重新绘制，渲染到屏幕上。如果在16ms内，主线程进行了耗时操作，CPU和GPU没有来得及生产出一帧缓冲，那么这一帧会被丢弃，显示器就会保持不变，继续显示上一帧内容，用户的视觉上就出现了卡顿；因此卡顿产生的原因就是，CPU和GPU没有及时处理好数据。 </p>
<h1 id="卡顿的检测"><a href="#卡顿的检测" class="headerlink" title="卡顿的检测"></a>卡顿的检测</h1><h2 id="卡顿分析工具"><a href="#卡顿分析工具" class="headerlink" title="卡顿分析工具"></a>卡顿分析工具</h2><p>从Xcode12 开始，Instrument 新增 AnimationHitches 用以检测卡顿,这里用它分析页面卡顿情况<br><img src="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E5%8D%A1%E9%A1%BF%E5%88%86%E6%9E%90%E5%B7%A5%E5%85%B7.png"></p>
<p> 苹果于 20 年的 Session 中提出了 Hitch 的概念，用以衡量滑动时的卡顿情况。Hitch 指的是 卡顿时间（一帧延后出现的时间，ms）/ 总时间（一般是 1 秒），简单来说 卡顿时间比就是一个区间内的总卡顿时间除以它的持续时间。<br><img src="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E5%8D%A1%E9%A1%BF%E6%AF%94%E4%B8%A5%E9%87%8D%E7%A8%8B%E5%BA%A6%E8%A7%86%E5%9B%BE.png"></p>
<ul>
<li>低于 5 ms/s 说明比较优秀，是最不易被用户察觉到的</li>
<li>介于 5ms/s  和 10ms/s 之间，说明发生了中等卡顿，用户会察觉到一些中断，但并不严重</li>
<li>高于 10 ms/s 说明发生了较严重的卡顿，已经影响了用户体验。<br>耗时分析工具</li>
</ul>
<p><img src="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E8%80%97%E6%97%B6%E5%88%86%E6%9E%90%E5%B7%A5%E5%85%B7.png"></p>
<p>选中某一次卡顿,结合Instrument - TimeProfile, 就可以定位到具体的函数堆栈                       </p>
<h2 id="页面卡顿检测"><a href="#页面卡顿检测" class="headerlink" title="页面卡顿检测"></a>页面卡顿检测</h2><p><img src="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/yemian.png"><br><img src="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E9%A1%B5%E9%9D%A2%E5%8D%A1%E9%A1%BF%E6%83%85%E5%86%B5.png"><br>大于16.6ms /s 的为较严重卡顿,大约有33个，有一些卡顿达到了116ms/s。</p>
<h2 id="某次卡顿分析"><a href="#某次卡顿分析" class="headerlink" title="某次卡顿分析"></a>某次卡顿分析</h2><p>查看某次（116ms/s。)卡顿函数主线程调用栈，查看其使用的时间占比:<br><img src="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E6%9F%90%E6%AC%A1%E5%8D%A1%E9%A1%BF%E5%9B%BE.png"></p>
<p><strong>可知耗时操作有：</strong></p>
<pre><code>AWHomeCommunityTagTableCell的 setModel 
UIImage+WebPConfig 的 aw_imageNamed方法、        
UIApplication+AWTAutomaticTracks的 awt_sendEvent 
NBSLens_iOSANREntity 的 runloopCycleStart 
AWHomeSearchNetManager 的 getRecommendListWithList 
map2JsonString 
...
</code></pre>
<h1 id="卡顿原因及优化"><a href="#卡顿原因及优化" class="headerlink" title="卡顿原因及优化"></a>卡顿原因及优化</h1><h2 id="动态卡setModel的耗时："><a href="#动态卡setModel的耗时：" class="headerlink" title="动态卡setModel的耗时："></a><strong>动态卡setModel的耗时：</strong></h2><ol>
<li>主线程卡片高度计算<br>滑动过程中视图的宽高,文本对象宽高,排版及绘制都有一定耗时</li>
</ol>
<p>优化:异步计算视图宽高,富文本对象宽高、排版并缓存到model里,这里富文本处理主要使用的是:<code>YYText</code> ,示例:</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">// 如果你在显示字符串时有性能问题，可以这样开启异步模式：</span><br><span class="line">YYLabel *label = ...</span><br><span class="line">label.displaysAsynchronously = YES;</span><br><span class="line">    </span><br><span class="line">// 如果需要获得最高的性能，你可以在后台线程用 `YYTextLayout` 进行预排版： </span><br><span class="line">YYLabel *label = [<span class="type">YYLabel</span> <span class="type">new</span>];</span><br><span class="line">label.displaysAsynchronously = YES; //开启异步绘制</span><br><span class="line">label.ignoreCommonProperties = YES; //忽略除了 textLayout 之外的其他属性</span><br><span class="line">    </span><br><span class="line">dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, <span class="number">0</span>), ^&#123;</span><br><span class="line">   // 创建属性字符串</span><br><span class="line">   NSMutableAttributedString *text = [[<span class="type">NSMutableAttributedString</span> <span class="type">alloc</span>] <span class="type">initWithString</span>:<span class="string">@&quot;Some Text&quot;];</span></span><br><span class="line"><span class="string">   text.yy_font = [UIFont systemFontOfSize:16];</span></span><br><span class="line"><span class="string">   text.yy_color = [UIColor grayColor];</span></span><br><span class="line"><span class="string">   [text yy_setColor:[UIColor redColor] range:NSMakeRange(0, 4)];</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string">   // 创建文本容器</span></span><br><span class="line"><span class="string">   YYTextContainer *container = [YYTextContainer new];</span></span><br><span class="line"><span class="string">   container.size = CGSizeMake(100, CGFLOAT_MAX);</span></span><br><span class="line"><span class="string">   container.maximumNumberOfRows = 0;</span></span><br><span class="line"><span class="string">   </span></span><br><span class="line"><span class="string">   // 生成排版结果</span></span><br><span class="line"><span class="string">   YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];</span></span><br><span class="line"><span class="string">   </span></span><br><span class="line"><span class="string">   dispatch_async(dispatch_get_main_queue(), ^&#123;</span></span><br><span class="line"><span class="string">       label.size = layout.textBoundingSize;</span></span><br><span class="line"><span class="string">       label.textLayout = layout;</span></span><br><span class="line"><span class="string">   &#125;);</span></span><br><span class="line"><span class="string">&#125;);</span></span><br></pre></td></tr></table></figure>

<p>2、网络请求数据缓存归档<br>数据的IO读写先对是比较耗时的<br>优化:改为异步的方式</p>
<p>3、不必要的操作<br>整个页面都是通过TableCell实现的,对于顶部的轮播卡,快捷入口等只展示一次的卡片在上下滑动过程中仍会reload,显然是多余的</p>
<p>优化:只对table的底部会复用的卡片通过UITableViewCell来实现，对于顶部分类tabbar、轮播卡、notice卡和快捷入口卡等只展示一次的卡片通过TableHeader来实现,避免花朵过程中多余的排版</p>
<p>4、滑动过程中的操作: 移除视图、初始化视图、添加视图<br><strong>优化</strong>：</p>
<ul>
<li>视图的初始化：懒加载的方式进行初始化，只初始化一次，如需要重置则可以在重用方法里重置。</li>
<li>移除视图、添加视图：通过hidden来控制视图的隐藏显示。</li>
</ul>
<p>5、xib、masonry、自动布局的使用</p>
<p>优化：纯代码方式创建cell，frame方式布局</p>
<p>6、一些对象在反复创建，或在滑动过程中拼接生成<br>如富文本内容、标签等</p>
<p>优化：将 NSAttributeString的初始化逻辑和数据源逻辑处理放在异步子线程中，然后缓存到model里。 </p>
<p>7、视图图层复杂<br>原代码用xib实现的卡片,一些自定义视图、活动卡片标签的实现等较为复杂</p>
<p>优化：尽量减少冗余视图，活动卡片标签改为富文本方式实现</p>
<p>8、native图片加载，会反复进行IO读取，并在主线程上的图片解码      <img src="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E6%9C%AC%E5%9C%B0%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD.png"><br>项目里用了组件化,通过hook <code>imageNamed</code>,查找相应组件的bundle,然后调YYImage的解码方式在主线程解码图片,这里的问题是在反复滑动过程中,会反复的查找bundle - 解码, 较为耗时</p>
<p>优化: 对解码过的图片进行LRU缓存，提高读取效率</p>
<p>9、高分辨率图的加载会造成一定卡顿</p>
<p>网络图片的加载过程:加载 - 解码 - 渲染 ,除此之外还会有IO读写 ,如果是高分辨率图,也是很耗内存和CPU的</p>
<p>优化主要有两种方式:</p>
<table>
<thead>
<tr>
<th align="left">方式</th>
<th align="left"></th>
<th align="left">特点</th>
<th align="left">支持</th>
</tr>
</thead>
<tbody><tr>
<td align="left">下采样加载</td>
<td align="left">根据size加载相应大小的图片,图片源不变</td>
<td align="left">只针对下载后的图片做的操作<br>不需要图片服务器支持</td>
<td align="left">SDWebimage支持,<br>YYWebImage需要自己扩展</td>
</tr>
<tr>
<td align="left">按需请求缩略图</td>
<td align="left">根据size请求相应大小的图片</td>
<td align="left">节约流量,<br>请求快,<br>降低内存占用<br>需要图片服务器支持</td>
<td align="left">YYWebImage支持<br>SDWebimage支持</td>
</tr>
</tbody></table>
<p>项目里图片服务器是用的阿里云OSS,支持图片缩放,故采用第二种方案,请求缩略图实例:</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://oss<span class="literal">-console-img-demo-cn-hangzhou</span>.oss<span class="literal">-cn-hangzhou</span>.aliyuncs.com/example.jpg?x<span class="literal">-oss-process</span>=image/resize,h_100,m_lfit</span><br></pre></td></tr></table></figure>
<p>10、轮播动画<br>icarousel step，轮播图等，滑出界面后，这个头部banner也一直在轮播，有一定耗时</p>
<p>优化: 在动画卡滑出界面或在离开当前页时，停止动画, 再出现后再开启</p>
<p>11、JPVideoplayerManager savePlaybackElapsedSeconds:forVideoUrl</p>
<p>视频播放过程会不断在主线程写文件,较为耗时</p>
<p>优化: 改为异步方式</p>
<p>12、离屏渲染问题<br>主要是针对如下场景：阴影、遮罩、组不透明等进行优化</p>
<p>该页面引起离屏幕渲染的问题主要是绘制阴影：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">self.customTabbr.layer.shadowOffset = CGSizeMake(5, 0);</span><br><span class="line">self.customTabbr.layer.shadowColor = [UIColor blackColor].CGColor;</span><br><span class="line">self.customTabbr.layer.shadowOpacity = 0.2;//阴影透明度，默认0</span><br><span class="line">self.customTabbr.layer.shadowRadius = 5;//阴影半径，默认3</span><br></pre></td></tr></table></figure>
<p>优化:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.customTabbr.bounds];</span><br><span class="line">self.customTabbr.layer.shadowPath = path.CGPath;</span><br></pre></td></tr></table></figure>

<h2 id="其它耗时"><a href="#其它耗时" class="headerlink" title="其它耗时"></a><strong>其它耗时</strong></h2><ol>
<li>UIApplication+AWTAutomaticTracks的 awt_sendEvent<br>这里是通过hook的方式对事件做了自动采集</li>
</ol>
<p>这个目前耗时占得比重不是很高，且影响较大,暂时没做优化</p>
<p>后续优化：出于性能考虑，会把这种自动埋点方式去掉，改成部分hook（比如页面pv）结合手动埋点方式搜集日志信息。</p>
<p>2、NBSLens_iOSANREntity 的 runloopCycleStart</p>
<p>这个是一个第三方sdk,用来监测线上crash</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[NBSAppAgent setStartOption: NBSOption_Crash | NBSOption_UI];</span><br></pre></td></tr></table></figure>
<p>优化:替换成性能更好的其它方案(bugly),且它支持卡顿分析<br>3、BaiduMobStat</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[[BaiduMobStat defaultStat] startWithAppId:@&quot;&quot;];</span><br></pre></td></tr></table></figure>
<p>百度热力图，占用耗时根听云sdk时差不多的，如果不是很需要可以考虑去掉<br>主要是用来分析用户行为的</p>
<p>优化: 后续重构数据采集sdk,一起替换掉</p>
<p>效果：<br><img src="https://raw.githubusercontent.com/shijianmei/blog_Images/main/%E4%B8%80%E6%AC%A1%E5%8D%A1%E9%A1%BF%E4%BC%98%E5%8C%96/%E4%BC%98%E5%8C%96%E6%95%88%E6%9E%9C.png"></p>
<p>可以看到卡顿比已经下降了很多</p>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考:"></a>参考:</h1><p><a target="_blank" rel="noopener" href="https://github.com/ibireme/YYText">https://github.com/ibireme/YYText</a></p>
<p><a target="_blank" rel="noopener" href="https://developer.apple.com/videos/play/tech-talks/10855/">https://developer.apple.com/videos/play/tech-talks/10855/</a></p>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/09/18/%E5%8D%A1%E9%A1%BF%E7%9B%91%E6%8E%A7/" rel="prev" title="卡顿监控">
      <i class="fa fa-chevron-left"></i> 卡顿监控
    </a></div>
      <div class="post-nav-item">
    <a href="/2023/01/16/%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%87%8D%E6%8E%92/" rel="next" title="二进制重排">
      二进制重排 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E5%87%BA%E7%8E%B0%E5%8D%A1%E9%A1%BF"><span class="nav-number">1.</span> <span class="nav-text">为什么出现卡顿</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8D%A1%E9%A1%BF%E7%9A%84%E6%A3%80%E6%B5%8B"><span class="nav-number">2.</span> <span class="nav-text">卡顿的检测</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%A1%E9%A1%BF%E5%88%86%E6%9E%90%E5%B7%A5%E5%85%B7"><span class="nav-number">2.1.</span> <span class="nav-text">卡顿分析工具</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%A1%B5%E9%9D%A2%E5%8D%A1%E9%A1%BF%E6%A3%80%E6%B5%8B"><span class="nav-number">2.2.</span> <span class="nav-text">页面卡顿检测</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9F%90%E6%AC%A1%E5%8D%A1%E9%A1%BF%E5%88%86%E6%9E%90"><span class="nav-number">2.3.</span> <span class="nav-text">某次卡顿分析</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8D%A1%E9%A1%BF%E5%8E%9F%E5%9B%A0%E5%8F%8A%E4%BC%98%E5%8C%96"><span class="nav-number">3.</span> <span class="nav-text">卡顿原因及优化</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8A%A8%E6%80%81%E5%8D%A1setModel%E7%9A%84%E8%80%97%E6%97%B6%EF%BC%9A"><span class="nav-number">3.1.</span> <span class="nav-text">动态卡setModel的耗时：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B6%E5%AE%83%E8%80%97%E6%97%B6"><span class="nav-number">3.2.</span> <span class="nav-text">其它耗时</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number">4.</span> <span class="nav-text">参考:</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Shi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">9</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/shijianmei" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;shijianmei" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        <!--

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Shi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>
-->

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
